package com.video.information.ui.information

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.video.information.Const
import com.video.information.data.model.Information
import com.video.information.extension.load
import com.video.information.ui.common.holder.ItemInformationHolderOen
import com.video.information.ui.common.holder.ItemInformationHolderThree
import com.video.information.ui.common.holder.ItemInformationHolderTwo
import com.video.information.ui.common.holder.RecyclerViewHelp

class InformationAdapter() :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    var informationData: List<Information.RECORDSBean>? = null

    constructor(informationData: List<Information.RECORDSBean>) : this() {
        this.informationData = informationData
    }

    override fun getItemViewType(position: Int): Int {
        val type = informationData?.get(position)?.type ?: TODO("type is null")
        return if (type == 1) Const.ITEM_INFORMATION_HOLDER1 else if (type == 2) Const.ITEM_INFORMATION_HOLDER2 else if (type == 3) Const.ITEM_INFORMATION_HOLDER3 else -1
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return RecyclerViewHelp.getViewHolder(parent, viewType)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val bean = informationData?.get(position) ?: TODO("informationData is null")
        when (getItemViewType(position)) {
            Const.ITEM_INFORMATION_HOLDER1 -> {
                val vh = holder as ItemInformationHolderOen
                vh.title.text = bean.news_title
                vh.author.text = bean.author_name
                val comment = "${bean.comment_count}评论 ."
                vh.comment.text = comment
                vh.header.load(bean.header_url, 25f)
                vh.thumb.load(bean.thumbEntities[0].thumb_url)
            }
            Const.ITEM_INFORMATION_HOLDER2 -> {
                val vh = holder as ItemInformationHolderTwo
                vh.title.text = bean.news_title
                vh.author.text = bean.author_name
                val comment = "${bean.comment_count}评论 ."
                vh.comment.text = comment
                vh.header.load(bean.header_url, 25f)
                vh.pic1.load(bean.thumbEntities[0].thumb_url)
                vh.pic1.load(bean.thumbEntities[1].thumb_url)
                vh.pic1.load(bean.thumbEntities[2].thumb_url)
            }
            Const.ITEM_INFORMATION_HOLDER3 -> {
                val vh = holder as ItemInformationHolderThree
                vh.title.text = bean.news_title
                vh.author.text = bean.author_name
                val comment = "${bean.comment_count}评论 ."
                vh.comment.text = comment
                vh.header.load(bean.header_url, 25f)
                vh.thumb.load(bean.thumbEntities[0].thumb_url)
            }
        }
    }

    override fun getItemCount(): Int {
        return if (informationData != null && informationData!!.isNotEmpty()) informationData!!.size else 0
    }
}